package com.ruoyi.web.controller.fileshare;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.domain.fileshare.dto.*;
import com.ruoyi.domain.fileshare.entity.MailUser;
import com.ruoyi.domain.fileshare.entity.SysFile;
import com.ruoyi.service.fileshare.FileShareService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件分享api
 * @author changc
 * */
@RestController
@RequestMapping("/file-share")
public class FileShareController extends BaseController{
    @Resource
    private FileShareService fileShareService;

    /**
     * 新增（上传文件信息）
     * */
    @PostMapping("/add")
    public R add(@RequestBody List<SysFileDto> dtoList) {
        return R.ok(fileShareService.addFileInfo(dtoList));
    }

    /**
     * 更新（上传文件信息）
     * */
    @PostMapping("/update")
    public R update(SysFileDto dto){
        return R.ok();
    }

    /**
     * 删除（软删除文件信息）
     * */
    @DeleteMapping("/delete/{ids}")
    public R remove(@PathVariable String ids){
        return R.ok(fileShareService.deleteFileInfo(ids));
    }

    /**
     * 获取当前登录用户元信息
     * */
    @GetMapping("/meta")
    public R getMeta(){
        return R.ok(fileShareService.getUserSupplierMeta());
    }
    /**
     * 通过邮件方式发送文件共享
     * */
    @PostMapping("/send")
    public R sendFiles(@RequestBody MailUserSendDto dto){
        return R.ok(fileShareService.sendFiles(dto));
    }

    /**
     * 可发送文件列表
     * */
    @GetMapping("/list")
    public R<PageInfo<SysFile>> listFiles(SysFileQueryDto queryDto){
        return R.ok(fileShareService.listFiles(queryDto));
    }

    /**
     * 可接收文件用户列表
     * */
    @GetMapping("/list-receiver")
    public R listReceivers(){
        return R.ok(fileShareService.listMailUser());
    }

    @PostMapping("/add-receiver")
    public R addMailUser(@RequestBody MailUserAddDto dto){
        return R.ok(fileShareService.addOrUpdateMailUser(dto));
    }

    @GetMapping("/detail/{id}")
    public R fileDetail(@PathVariable Long id){
        return R.ok(fileShareService.getFileShareDetail(id));
    }

    @GetMapping("/receiver/list")
    public R<PageInfo<MailUser>> listReceiver(MailUserQueryDto queryDto){
        return R.ok(fileShareService.listMailUser(queryDto));
    }

    @GetMapping("/receiver/select-list")
    public R<List<MailUser>> listSelectReceiver(){
        return R.ok(fileShareService.listSelectMailUser());
    }

    @DeleteMapping("/receiver/delete/{id}")
    public R receiverDelete(@PathVariable Long id){
       return R.ok(fileShareService.deleteMailUser(id));
    }

    @PostMapping("/receiver/download-log")
    public R downloadLog(@RequestBody SysFile psDto){
        return R.ok(fileShareService.downloadLog(psDto));
    }

    @PostMapping("/submit-share")
    public R submitShare(@RequestBody FileShareSubmitDto dto){
        if(dto.getIsAdd()){
            var fileIds = List.of(dto.getFileId());
            fileShareService.addFileShare(fileIds,dto.getUserIdList());
        }else{
            fileShareService.cancelShare(dto);
        }
        return R.ok();
    }

    /**
     * 获取已分享的文件用户信息列表或未分享的文件用户信息列表
     * @param isAdd true:未分享的文件用户信息列表，false:已分享的文件用户信息列表
     * @param fileId 文件id
     * */
    @GetMapping("/share-list")
    public R shareList(@RequestParam Long fileId,@RequestParam Boolean isAdd){
        return R.ok(fileShareService.getShareableList(fileId,isAdd));
    }

    @GetMapping("/user-charge-supplier-list")
    public R getUserChargeSupplierList(){
        return R.ok(fileShareService.getUserChargeSupplierList());
    }
}
